package com.dhecp.project.mall.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.core.utils.DateUtil;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.mall.domain.SignLog;
import com.dhecp.project.mall.mapper.SignLogMapper;
import com.dhecp.project.mall.service.SignLogService;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 学分商城控制器
 */
@Controller
@RequestMapping("/mall")
public class MallController extends BaseModuleController{

    @Autowired
    private SignLogMapper signLogMapper;
    @Autowired
    private SignLogService signLogService;

    /**
     * 获取签到信息
     */
    @PostMapping("/getSignInfo")
    @ResponseBody
    public AjaxResult getSignInfo(){

		String userId = ShiroUtils.getUserId();
		SignLog signLog = signLogMapper.queryLatest(userId);
		String tips = "";
		Integer signAble = 1;
		if (signLog == null) {
			tips = "尚未签到";
		} else {
			tips = "上次签到时间：" + DateUtil.getYYYY_MM_DD_HH_mm_ss(signLog.getCreateTime());
			Date now = new Date();
			if (DateUtil.addSecond(now, -3600 * 6).compareTo(signLog.getCreateTime()) < 0) {
				signAble = 0;
			}
		}
		
		GetSignInfoDto dto = new GetSignInfoDto(tips, signAble);
        return AjaxResult.success(dto);
    }
	@Data
	@AllArgsConstructor
	private static class GetSignInfoDto{
		private String tips;
		private Integer signAble;
	}

    /**
     * 签到
     */
    @PostMapping("/sign")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult sign(){
    	
    	String loginUserId = ShiroUtils.getUserId();
    	String userName = ShiroUtils.getUserName();

        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= signLogService.add(loginUserId, userName);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
